

function Rec(x,y,width,height){
    
    this.x=x;
    this.y=y;
    this.width=width;
    this.height=height
    
    this.realW=this.width*10+10;
    this.realH=this.height*10+10;

    this.realX=this.x*10;
    this.realY=this.y*10;
    
    this.canvas=document.createElement('canvas');
    this.img=new Image();
    this.img.src='./img/room.png';
    
    

    
    this.render();
}
Rec.prototype={
   
    render:function(){
       
        this.canvas.width=MAX_X*10+10;
        this.canvas.height=MAX_Y*10+10;
        var ctx=this.canvas.getContext('2d');
        
        var self=this;
       
        this.img.onload=function(){
             ctx.drawImage(self.img,0,0,self.realW,self.realH,self.realX,self.realY,self.realW,self.realH);
             
        }
       
        
    },
    inValid:function(){
        if(this.y+this.height>MAX_Y||this.x+this.width>MAX_X){
            return true
        }
        var sX=this.x-2>0?this.x-2:(this.x-1>0?this.x-1:this.x);
        var eX=this.x+this.width+2<MAX_X?this.x+this.width+2:(this.x+this.width+1<MAX_X?this.x+this.width+1:this.x+this.width);

        var sY=this.y-2>0?this.y-2:(this.y-1>0?this.y-1:this.y);
        var eY=this.y+this.height+2<MAX_Y?this.y+this.height+2:(this.y+this.height+1<MAX_Y?this.y+this.height+1:this.y+this.height);

        
        for(var i=sX;i<eX;i++){
           
            if(getSqu(i,sY).filled==true){
                return true;
            }
        };
        for(var i=sX;i<eX;i++){
             if(!getSqu(i,eY)){
                 console.log('sY='+sY);
                 console.log('height='+this.height)
                 console.log('ex='+eX)
                console.log('eY='+eY);
                console.log('i='+i)
            }
            if(getSqu(i,eY).filled==true){
                return true;
            }
        };
        if(sY+minH<eY){
             for(var i=sX;i<eX;i++){
                if(getSqu(i,sY+minH).filled==true){
                    return true;
                }
            }
        };
        if(sY+minH*2<eY){
             for(var i=sX;i<eX;i++){
                if(getSqu(i,sY+minH*2).filled==true){
                    return true;
                }
            }
        }
        return false;
    
    },
    //为creRoom设置的检验是否合法的方法
    inValid2:function(){
        var sX=this.x-2>0?this.x-2:(this.x-1>0?this.x-1:this.x);
        var eX=this.x+this.width+2<MAX_X?this.x+this.width+2:(this.x+this.width+1<MAX_X?this.x+this.width+1:this.x+this.width);
        var sY=this.y-2>0?this.y-2:(this.y-1>0?this.y-1:this.y);
        var eY=this.y+this.height+2<MAX_Y?this.y+this.height+2:(this.y+this.height+1<MAX_Y?this.y+this.height+1:this.y+this.height);
        for(var i=sX;i<eX;i++){
            if(getSqu(i,eY).filled==true){
                return true;
            }
        };
        if(sY+minH<eY){
             for(var i=sX;i<eX;i++){
                if(getSqu(i,sY+minH).filled==true){
                    return true;
                }
            }
        };
        if(sY+minH*2<eY){
             for(var i=sX;i<eX;i++){
                if(getSqu(i,sY+minH*2).filled==true){
                    return true;
                }
            }
        }
        return false;
    
    }
    

}


var backRec=new Rec(0,0,151,81);
backRec.img.src='./img/back.png';
backRec.render();

var squList=[];
for(var i=0;i<MAX_X+1;i++){
    squList[i]=[];
    for(var j=0;j<MAX_Y+1;j++){
        squList[i][j]=new Squ(i,j);
    }
}



var emptyList=[];
for(var i=0;i<squList.length;i++){
    emptyList[i]=[];
    emptyList[i]=squList[i].slice(0);
}

var filledList=[];
for(var i=0;i<MAX_X+1;i++){
    filledList[i]=[];
    
}



function getSqu(x,y){
    if(x>MAX_X||y>MAX_Y||x<0||y<0){
        return "";
    }
     
     return squList[x][y]
}


